# the minimum version of CMake.
cmake_minimum_required (VERSION 3.4.1)
project (marsnapi)

set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "Installation directory" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(CMAKE_C_STANDARD 11)

message(STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")

set(NAPI_MARS_PATH ${CMAKE_SOURCE_DIR}/mars/mars)
set(SELF_LIB_NAME marsnapi)
if(OHOS_ARCH MATCHES "armeabi-v7a")
       set(ANDROID_ABI "armeabi-v7a")
elseif(OHOS_ARCH MATCHES "arm64-v8a")
       set(ANDROID_ABI "arm64-v8a")
elseif(OHOS_ARCH MATCHES "x86_64")
       set(ANDROID_ABI "x86_64")
endif() # OHOS_ARCH MATCHES "armeabi-v7a"

add_definitions(-DOHOS_MARS_BUILD)
add_definitions (-Wno-format-security)

include_directories(${NAPI_MARS_PATH})
include_directories(${NAPI_MARS_PATH}/openssl/include)
include_directories(${CMAKE_SOURCE_DIR}/thirdparty/openssl/${ANDROID_ABI}/include)
add_subdirectory(${NAPI_MARS_PATH}/comm comm)
add_subdirectory(${NAPI_MARS_PATH}/boost boost)
add_subdirectory(${NAPI_MARS_PATH}/app app)
add_subdirectory(${NAPI_MARS_PATH}/baseevent baseevent)
add_subdirectory(${NAPI_MARS_PATH}/log xlog)
add_subdirectory(${NAPI_MARS_PATH}/sdt sdt)
add_subdirectory(${NAPI_MARS_PATH}/stn stn)

# for zstd
option(ZSTD_BUILD_STATIC "BUILD STATIC LIBRARIES" ON)
option(ZSTD_BUILD_SHARED "BUILD SHARED LIBRARIES" OFF)
set(ZSTD_SOURCE_DIR "${NAPI_MARS_PATH}/zstd")
set(LIBRARY_DIR ${ZSTD_SOURCE_DIR}/lib)
include(GNUInstallDirs)
add_subdirectory(${NAPI_MARS_PATH}/zstd/build/cmake/lib zstd)

include(${NAPI_MARS_PATH}/comm/CMakeUtils.txt)
include_directories(.)
include_directories(..)

set(SELF_LIBS_OUT ${CMAKE_SYSTEM_NAME}.out)
set(CMAKE_BUILD_WITH_INSTALL_RPATH true)

if(OHOS)
    find_library(log-lib log)
    find_library(z-lib z)
    link_directories(
                    ${NAPI_MARS_PATH}/app
                    ${NAPI_MARS_PATH}/baseevent
                    ${NAPI_MARS_PATH}/sdt
                    ${NAPI_MARS_PATH}/stn
                    ${NAPI_MARS_PATH}/comm
                    ${NAPI_MARS_PATH}/boost
                    ${NAPI_MARS_PATH}/xlog
                    ${NAPI_MARS_PATH}/zstd)
    file(GLOB NAPI_INTERFACES napi_interfaces/*.cc)
    file(GLOB DEMO_NAPI_FILES demonapi/*.cpp)
    file(GLOB SELF_SRC_FILES ${NAPI_MARS_PATH}/libraries/mars_ohos_sdk/napi/*.cc)
    add_library(${SELF_LIB_NAME} SHARED
                ${NAPIDEMO_SOURCE_FILES}
                ${SELF_SRC_FILES}
                ${DEMO_NAPI_FILES}
                ${NAPI_INTERFACES})
    install(TARGETS ${SELF_LIB_NAME} LIBRARY DESTINATION ${SELF_LIBS_OUT} ARCHIVE DESTINATION ${SELF_LIBS_OUT})
    link_directories(${SELF_LIBS_OUT})
    if(NOT MSVC)
        set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
        set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
    endif() # NOT MSVC
    find_library(CRYPT_LIB crypto PATHS ${CMAKE_SOURCE_DIR}/thirdparty/openssl/${ANDROID_ABI}/lib NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
    find_library(SSL_LIB ssl PATHS ${CMAKE_SOURCE_DIR}/thirdparty/openssl/${ANDROID_ABI}/lib NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
    target_link_libraries(${SELF_LIB_NAME} PUBLIC
                            libace_napi.z.so
                            libc++.a
                            libcrypt.a
                            libhilog_ndk.z.so
                            stn
                            sdt
                            app
                            baseevent
                            comm
                            mars-boost
                            xlog
                            libzstd_static
							${SSL_LIB}
                            ${CRYPT_LIB}
                            uv)
endif() # OHOS
